package com.haochang.chunkvod.controller.adapter.order;

import java.util.List;

import com.haochang.base.BaseTextView;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.utils.ScreenUtils;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.hostess.GoodsFeeInfo;
import com.haochang.chunkvod.tools.vod.other.AppendImageUtil;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;

import android.content.Context;
import android.graphics.Paint;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * 点餐账单adapter
 *
 * @author Yangjie
 *         className OrderBillAdapter
 *         created at  2016/11/4  11:51
 */
public class OrderBillAdapter extends QuickAdapter<GoodsFeeInfo>
{
	private Context context;
	private int keyOfItemTag = R.id.clicked_item;
	private int keyOfView = R.id.item_view;//点击传递一个view过去

	public OrderBillAdapter(Context context, int layoutResId, List<GoodsFeeInfo> list)
	{
		super(context, layoutResId, list);
		this.context = context;
	}

	@Override
	protected void convert(BaseAdapterHelper helper, GoodsFeeInfo item)
	{
		if (item != null)
		{
			LinearLayout linearContainer = helper.getView(R.id.linearContainer);
			BaseTextView tv_goods_money = helper.getView(R.id.tv_goods_money);
			BaseTextView tv_goods_name = helper.getView(R.id.tv_goods_name);
			ImageView iv_expand = helper.getView(R.id.iv_expand);//展开imageView
			tv_goods_money.setText(String.valueOf(item.getMoney() / 100));
			tv_goods_name.setText(!TextUtils.isEmpty(item.getGoodsName()) ? item.getGoodsName() : "");
			//设置商品名的最大宽度限制 ：24dp是左右边距，5/9是tv_goods_name最大比例，60px是展开的imageView的像素,取40dp
			tv_goods_name
					.setMaxWidth((ScreenUtils.getScreenWidth(context) - DipPxConversion.dip2px(context, 24)) * 5 / 9 - DipPxConversion.dip2px(context, 40));
			helper.setText(R.id.tv_goods_count, item.getType() == 2 ? "" : item.getCount() + item.getUnit());
			if (item.is_give())//赠送
			{
				tv_goods_name.append(AppendImageUtil.addImageSpan(context, R.drawable.ic_gift, 10));
				tv_goods_money.setTextColor(context.getResources().getColor(R.color.color_f04e39));
				tv_goods_money.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
				tv_goods_money.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
			} else if (item.getMoney() < 0 || item.getCount() < 0)
			{//这个是退单商品
				tv_goods_name.setText(Html.fromHtml("<font color=#f04e39>" + (!TextUtils.isEmpty(item.getGoodsName()) ? item.getGoodsName() : "") + "</font>"));
				helper.setText(R.id.tv_goods_count,
						item.getType() == 2 ? "" : Html.fromHtml("<font color=#f04e39>" + item.getCount() + item.getUnit() + "" + "</font>"));
				tv_goods_money.setText(Html.fromHtml("<font color=#f04e39>" + String.valueOf(item.getMoney() / 100) + "</font>"));
				tv_goods_money.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
				tv_goods_money.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
			} else//默认 正常的单
			{
				tv_goods_money.setTextColor(context.getResources().getColor(R.color.color_282828));
				tv_goods_money.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_gray), null, null, null);
				tv_goods_money.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
			}
			linearContainer.removeAllViews();
			if (item.getType() != 0)
			{
				//动态addview
				if (item.getArrayList().size() > 0)
				{
					int len = item.getArrayList().size();
					for (int i = 0; i < len; i++)
					{
						final ViewGroup viewGroup = null;
						View secondView = LayoutInflater.from(context).inflate(R.layout.item_consume_info_second, viewGroup, false);
						addView(context, item.getArrayList().get(i), linearContainer, secondView);
					}
				}
			}
			iv_expand.setVisibility(((item.getType() == 1 || item.getType() == 2) && linearContainer.getChildCount() > 0) ? View.VISIBLE : View.GONE);
			iv_expand.setImageResource(item.isShowBottom() ? R.drawable.up : R.drawable.down);
			linearContainer.setVisibility(item.isShowBottom() ? View.VISIBLE : View.GONE);
			iv_expand.setTag(keyOfItemTag, item);
			iv_expand.setTag(keyOfView, linearContainer);
			iv_expand.setOnClickListener(onBaseClickListener);
		}
	}

	/**
	 * addview
	 */
	private void addView(Context context, GoodsFeeInfo item, LinearLayout container, View inflateView)
	{
		if (item != null)
		{
			BaseTextView tv_goods_name = (BaseTextView) inflateView.findViewById(R.id.tv_goods_name);
			BaseTextView tv_goods_count = (BaseTextView) inflateView.findViewById(R.id.tv_goods_count);
			BaseTextView tv_goods_money = (BaseTextView) inflateView.findViewById(R.id.tv_goods_money);
			if (item.getMoney() < 0 || item.getCount() < 0)
			{//这个是退单的
				tv_goods_money.setVisibility(View.VISIBLE);
				tv_goods_name.setText(Html.fromHtml("<font color=#f04e39>" + (!TextUtils.isEmpty(item.getGoodsName()) ? item.getGoodsName() : "") + "</font>"));
				tv_goods_count.setText(item.getType() == 2 ? "" : Html.fromHtml("<font color=#f04e39>" + item.getCount() + item.getUnit() + "" + "</font>"));
				tv_goods_money.setText(Html.fromHtml("<font color=#f04e39>" + String.valueOf(item.getMoney() / 100) + "</font>"));
				tv_goods_money.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.doller_red), null, null, null);
				tv_goods_money.getPaint().setFlags(Paint.ANTI_ALIAS_FLAG);
			} else
			{
				tv_goods_money.setVisibility(View.INVISIBLE);
				tv_goods_name.setText(!TextUtils.isEmpty(item.getGoodsName())//字符个数超过14时加省略号
						? item.getGoodsName() : "");
				tv_goods_money.setText(String.valueOf(item.getMoney() / 100));
				tv_goods_count.setText(item.getType() == 2 ? "" : item.getCount() + item.getUnit());
			}
			if (item.getArrayList().size() > 0)
			{
				int len = item.getArrayList().size();
				for (int i = 0; i < len; i++)
				{
					GoodsFeeInfo info = item.getArrayList().get(i);
					LinearLayout linearContainer = (LinearLayout) inflateView.findViewById(R.id.linearContainer);
					try
					{
						final ViewGroup viewGroup = null;
						LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.item_consume_info_third, viewGroup, false);
						addView(context, info, linearContainer, linearLayout);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
				}
			}
			container.addView(inflateView);
		}
	}

	View.OnClickListener onBaseClickListener = new View.OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			GoodsFeeInfo item = (GoodsFeeInfo) v.getTag(keyOfItemTag);
			LinearLayout linearContainer = (LinearLayout) v.getTag(keyOfView);
			if (null != item && null != linearContainer)
			{
				boolean isShown = linearContainer.isShown();//点击之前的状态
				linearContainer.setVisibility(isShown ? View.GONE : View.VISIBLE);
				item.setShowBottom(!isShown);
				((ImageView) v).setImageResource(isShown ? R.drawable.down : R.drawable.up);
			}
		}
	};
}
